package com.ylsd.dynamic.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ylsd.dynamic.entity.DynamicCatalogin;
import com.ylsd.dynamic.entity.DynamicList;
import com.ylsd.dynamic.entity.School;
import com.ylsd.dynamic.mapper.DynamicMapper;
import com.ylsd.dynamic.service.DynamicService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.ylsdxy.model.response.CustomPage;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DynamicServiceImpl implements DynamicService {
    @Resource
    private DynamicMapper dynamicMapper;
    @Override
    public CustomPage<List<DynamicCatalogin>> findByPage(DynamicCatalogin dynamicCatalogin) {
        PageHelper.startPage(dynamicCatalogin.getPage(), dynamicCatalogin.getSize());
        List<DynamicCatalogin> dynamicCataloginList = dynamicMapper.findByPage(dynamicCatalogin);
        PageInfo<DynamicCatalogin> pageInfo = new PageInfo<>(dynamicCataloginList);

        CustomPage<List<DynamicCatalogin>> customPage = CustomPage.<List<DynamicCatalogin>>builder()
                .currentPage(dynamicCatalogin.getPage())
                .size(dynamicCatalogin.getSize())
                .totalPage(pageInfo.getPages())
                .total(pageInfo.getTotal())
                .data(dynamicCataloginList)
                .build();
        return customPage;
    }
    @Override
    public boolean insertDC(DynamicCatalogin dynamicCatalogin) {
        boolean result = dynamicMapper.insertDC(dynamicCatalogin);
        if (result){
            Integer dynamicId = dynamicMapper.findByDynamicId(dynamicCatalogin.getSystematicName());
            dynamicMapper.insertDynamicIdAndCampusId(dynamicCatalogin.getSchoolId(),dynamicId);
            return result;
        }else {
            return false;
        }
    }

    @Override
    public boolean delDC(List<Integer> id) {
        return dynamicMapper.delDC(id);
    }

    @Override
    public boolean updatetDC(DynamicCatalogin dynamicCatalogin) {
        return dynamicMapper.updatetDC(dynamicCatalogin);
    }

    @Override
    public CustomPage<List<DynamicList>> findDLByPage(DynamicList dynamicList) {
        PageHelper.startPage(dynamicList.getPage(),dynamicList.getSize());
        List<DynamicList> list = dynamicMapper.findDLByPage(dynamicList);
        PageInfo<DynamicList> listPageInfo = new PageInfo<>(list);

        CustomPage<List<DynamicList>> customPage = CustomPage.<List<DynamicList>>builder()
                .currentPage(dynamicList.getPage())
                .size(dynamicList.getSize())
                .totalPage(listPageInfo.getPages())
                .total(listPageInfo.getTotal())
                .data(list)
                .build();

        return customPage;
    }

    @Transactional
    @Override
    public boolean insertDL(DynamicList dynamicList) {
        boolean result = dynamicMapper.insertDL(dynamicList);
        dynamicMapper.updateByDynamicNum(dynamicList.getDcId());
        return result;
    }

    @Transactional
    @Override
    public boolean updateDL(DynamicList dynamicList) {
        boolean result = dynamicMapper.updateDL(dynamicList);
        return result;
    }

    @Transactional
    @Override
    public boolean delDL(List<Integer> id) {
        for (Integer i : id) {
            Integer dcid = dynamicMapper.findDCId(i);
            dynamicMapper.delDynamicNum(dcid);
        }
        return dynamicMapper.delDL(id);
    }

    @Override
    public List<School> choiceSchool() {
        return dynamicMapper.choiceSchool();
    }

    @Override
    public List<DynamicCatalogin> search(Integer id) {
        return dynamicMapper.search(id);
    }

    @Override
    public List<DynamicCatalogin> choiceDynamic() {
        return dynamicMapper.choiceDynamic();
    }

    @Override
    public String findImgById() {
        return dynamicMapper.findImgById();
    }
}
